using System;
using System.Collections.Generic;
using dotnet;
using ssh;
using System.IO;
using Renci.SshNet.Sftp;

namespace wspace
{
    public class ServerCmp
    {
        private SftpConnect Brg;
        private string ServerPath;
        private string LocalPath;

        public List<string> RmodifyList {get;}   /* Remote modification list, 服务器端修改的文件列表 */
        public List<string> LmodifyList {get;}   /* Local modification list, 本地端修改的文件列表 */
        public List<string> RnewFList {get;}     /* Remote new file list, 服务器端新增的文件列表 */

        private List<string> LignoreList = null;

        public ServerCmp(SftpConnect brg, string serverpath, string localpath)
        {
            this.Brg = brg;
            this.ServerPath = serverpath;
            this.LocalPath = localpath;

            this.RmodifyList = new List<string>(0);
            this.LmodifyList = new List<string>(0);
            this.RnewFList = new List<string>(0);
        }

        public bool CmpServerDiff(List<string> ignorelist)
        {
            this.LignoreList = ignorelist;
            bool ret = true;
            this.RmodifyList.Clear();
            this.LmodifyList.Clear();
            this.RnewFList.Clear();
            try
            {
                this.IteratingRemoteDir(this.ServerPath);
            }
            catch (Exception e)
            {
                Utils.ErrorLog(e.Message);
                ret = false;
            }
            return ret;
        }

        private void IteratingRemoteDir(string spath)
        {
            foreach(SftpFile dof in this.Brg.sftp.ListDirectory(spath))
            {
                string lpath = GenerateLocalPath(Utils.FormatPath(dof.FullName));
                if (dof.IsDirectory)
                {
                    if (dof.Name.Equals(".") || dof.Name.Equals(".."))
                    {
                        continue;
                    }
                    if (Directory.Exists(lpath))
                    {
                        if (!LignoreList.Contains(lpath + Utils.GetSlash()))
                        {
                            IteratingRemoteDir(dof.FullName);
                        }
                    }
                    else
                    {
                        this.RnewFList.Add(dof.FullName + "/");
                    }
                }
                else
                {
                    if (File.Exists(lpath))
                    {
                        if (!LignoreList.Contains(lpath))
                        {
                            DateTime rwdate = dof.LastWriteTime;
                            DateTime lwdate = Directory.GetLastWriteTime(lpath);
                            int timecmp = Utils.TimeCmp(rwdate, lwdate);
                            if (timecmp != 0)
                            {
                                if (timecmp > 0)
                                {
                                    this.RmodifyList.Add(dof.FullName);
                                }
                                else
                                {
                                    this.LmodifyList.Add(lpath);
                                }
                            }
                        }
                    }
                    else
                    {
                        this.RnewFList.Add(dof.FullName);
                    }
                }
            }
        }

        private string GenerateLocalPath(string remote)
        {
            string local = remote.Substring(this.ServerPath.Length + 1);
            return this.LocalPath + Utils.GetSlash() + local;
        }

    }
}